﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using WebDavProcessor.Constants;
using WebDavProcessor.Interfaces;
using WebDavProcessor.Resources;

namespace WCFServer.DataProviders
{
    class FileSystemProvider : IDataProvider
    {
        string root;

        public FileSystemProvider(string directoryName)
        {
            root = directoryName;
        }

        public bool IsExist(string resourcePath)
        {
            FileInfo fi = new FileInfo(Path.Combine(root, resourcePath));
            return fi.Exists;
        }

        private System.IO.Stream read(string resourcePath)
        {
            return read(resourcePath, 0, -1);
        }

        private System.IO.Stream read(string resourcePath, int offset, int count)
        {
            try
            {
                FileStream fs = File.OpenRead(Path.Combine(root, resourcePath));
                byte[] data = new byte[fs.Length];
                if (count < 0)
                    count = data.Length;
                fs.Read(data, offset, count);
                fs.Close();

                MemoryStream ms = new MemoryStream(data);
                return ms;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //public void Write(string resourcePath, System.IO.Stream data)
        //{
        //    FileStream fs = File.OpenWrite(Path.Combine(root, resourcePath));
        //    data.CopyTo(fs);
        //    fs.Close();
        //}

        //public System.Collections.Specialized.NameValueCollection GetAllProperties(string resourcePath)
        //{
        //    FileInfo finfo = new FileInfo(Path.Combine(root, resourcePath));
        //    System.Collections.Specialized.NameValueCollection props = new System.Collections.Specialized.NameValueCollection();
        //    props.Add("CreationTime", finfo.CreationTime.ToString());
        //    props.Add("Extension", finfo.Extension.ToString());
        //    props.Add("LastAccessTime", finfo.LastAccessTime.ToString());
        //    props.Add("LastWriteTime", finfo.LastWriteTime.ToString());
        //    props.Add("Length", finfo.Length.ToString());

        //    return props;
        //}

        //public System.Collections.Specialized.NameValueCollection GetAllProperties(string resourcePath, string[] includedNames)
        //{
        //    throw new NotImplementedException();
        //}

        //public System.Collections.Specialized.NameValueCollection GetProperties(string resourcePath, string[] propertyNames)
        //{
        //    throw new NotImplementedException();
        //}

        //public string[] GetPropertyNames(string resourcePath)
        //{
        //    return new string[] { "CreationTime", "Extension" ,
        //                                  "LastAccessTime", "LastWriteTime",  
        //                                  "Length"};
        //}


        public WebDavProcessor.Resources.WebDavResource Read(string resourcePath)
        {
            WebDavStreamResource res;
            try
            {
                Stream str = read(resourcePath);
                res = new WebDavStreamResource(resourcePath, str);
                
            }
            catch
            {
                res = new WebDavStreamResource(resourcePath, WebDavStatusCodes.Conflict ,null);
            }
            return res;
            
        }

        public WebDavProcessor.Resources.WebDavResource Read(string resourcePath, int offset, int count)
        {
            WebDavStreamResource res;
            try
            {
                Stream str = read(resourcePath, offset, count);
                res = new WebDavStreamResource(resourcePath, str);
            }
            catch
            {
                res = new WebDavStreamResource(resourcePath, WebDavStatusCodes.Conflict, null);
            }
            return res;
        }

        public WebDavProcessor.Resources.WebDavResource Write(string resourcePath, Stream data)
        {
            WebDavStreamResource res;
            res = new WebDavStreamResource(resourcePath, WebDavStatusCodes.NotImplemented, null);
            return res;
        }

        public WebDavProcessor.Resources.WebDavResource Delete(string resourcePath)
        {
            WebDavStreamResource res;
            res = new WebDavStreamResource(resourcePath, WebDavStatusCodes.NotImplemented, null);
            return res;
        }

        public WebDavProcessor.Resources.WebDavResource CreateCollection(string resourcePath)
        {
            WebDavStreamResource res;
            res = new WebDavStreamResource(resourcePath, WebDavStatusCodes.NotImplemented, null);
            return res;
        }

        public WebDavProcessor.Resources.WebDavResource Copy(string resourcePath, string destinationPath)
        {
            WebDavStreamResource res;
            res = new WebDavStreamResource(resourcePath, WebDavStatusCodes.NotImplemented, null);
            return res;
        }

        public WebDavProcessor.Resources.WebDavResource Move(string resourcePath, string destinationPath)
        {
            WebDavStreamResource res;
            res = new WebDavStreamResource(resourcePath, WebDavStatusCodes.NotImplemented, null);
            return res;
        }
    }
}
